from rest_framework import serializers
from ..models import FaKuyouWorkshop
from datetime import datetime


# 车间表
class FaKuyouWorkshopSerializer(serializers.ModelSerializer):
    status_display = serializers.CharField(
        source='get_status_display',
        read_only=True,
        help_text='状态显示文本'
    )

    class Meta:
        model = FaKuyouWorkshop
        fields = '__all__'
        extra_kwargs = {
            'code': {
                'error_messages': {
                    'blank': '车间编号不能为空',
                    'max_length': '车间编号长度不能超过30个字符'
                }
            },
            'name': {
                'error_messages': {
                    'blank': '车间名称不能为空',
                    'max_length': '车间名称长度不能超过255个字符'
                }
            }
        }

    def validate_code(self, value):
        """验证车间编号"""
        if not value.strip():
            raise serializers.ValidationError("车间编号不能为空")
        return value.strip()

    def validate_name(self, value):
        """验证车间名称"""
        if not value.strip():
            raise serializers.ValidationError("车间名称不能为空")
        return value.strip()
